import sys

from PyQt6.QtCore import QTimer, QTime, QDate, QRect, Qt, QPoint
from PyQt6.QtGui import QPolygon, QColor, QPainter, QFont, QRegion

from PyQt6.QtWidgets import QWidget, QApplication


class Clock(QWidget):
    hourShape = QPolygon([QPoint(6, 10), QPoint(-6, 10), QPoint(0, -45)])
    minuteShape = QPolygon([QPoint(6, 10), QPoint(-6, 10), QPoint(0, -70)])
    hourColor = QColor(0, 255, 0)
    minuteColor = QColor(0, 0, 255)
    secondColor = QColor(255, 0, 0)
    ymd = ''

    def __init__(self, parent=None):
        super(Clock, self).__init__(parent)
        self.setWindowTitle("时钟")
        timer = QTimer(self)
        timer.timeout.connect(self.update)
        timer.start(16)

    def paintEvent(self, event):
        time = QTime.currentTime()
        date = QDate.currentDate()
        year = date.year()
        month = date.month()
        day = date.day()
        self.ymd = str(year) + '年' + str(month) + '月' + str(day) + '日'
        self.painter = QPainter(self)

        self.drawTextDate(self.ymd)

        self.painter.setPen(self.hourColor)
        for e in range(12):
            self.painter.drawLine(88, 0, 96, 0)
            self.painter.rotate(30.0)

        self.painter.setPen(self.minuteColor)
        for e in range(60):
            if (e % 5) != 0:
                self.painter.drawLine(94, 0, 96, 0)
            self.painter.rotate(6.0)

        self.painter.setPen(Qt.PenStyle.NoPen)
        self.painter.setBrush(self.hourColor)
        self.painter.save()
        self.painter.rotate(30.0 * (time.hour() + time.minute() / 60.0))
        self.painter.drawPolygon(self.hourShape)
        self.painter.restore()

        self.painter.setPen(Qt.PenStyle.NoPen)
        self.painter.setBrush(self.minuteColor)
        self.painter.save()
        self.painter.rotate(6.0 * (time.minute() + time.second() / 60.0))
        self.painter.drawPolygon(self.minuteShape)
        self.painter.restore()

        self.painter.setPen(Qt.PenStyle.NoPen)
        self.painter.setBrush(self.secondColor)
        self.painter.drawEllipse(-4, -4, 8, 8)
        self.painter.save()
        self.painter.rotate(6.0 * (time.second() + time.msec() /1000.0))
        self.painter.drawRoundedRect(-1, -1, 80, 2, 2, 2)
        self.painter.restore()

    def drawTextDate(self, ymd):
        rect = QRect(220, 150, 200, 30)
        self.painter.setFont(QFont('黑体', 24))
        self.painter.drawText(rect, Qt.AlignmentFlag.AlignCenter, ymd)
        self.painter.setRenderHint(QPainter.RenderHint.Antialiasing)
        side = min(self.width(), self.height())
        self.painter.translate(self.width() / 2, self.height() / 2)
        self.painter.scale(side / 200.0, side / 200.0)

    # def resizeEvent(self, event):
    #     w = self.width()
    #     h = self.height()
    #     side = min(w, h)
    #     mask = QRegion(w / 2 - side / 2, h / 2 - side / 2, side, side, QRegion.RegionType.Ellipse)
    #     self.setMask(mask)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    clock = Clock()
    clock.show()
    sys.exit(app.exec())
